package com.cn.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.gmall.common.result.Result;
import com.cn.gmall.model.product.*;
import com.cn.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class BaseManageController {
    @Autowired
    ManageService manageService;

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        manageService.onLineSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelLineSale(skuId);
        return Result.ok();
    }


    /**
     * sku分页 显示
     * @param page 第几页
     * @param limit 一页多少个
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit) {

        Page<SkuInfo> pageParam = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = manageService.getSkuPage(pageParam);
        return Result.ok(pageModel);
    }



    /**
     * 根据平台属性Id 获取到平台属性值集合
     * /admin/product/getAttrValueList/{attrId}
     * 方式一直接 去BaseAttrValue表中读会产生脏数据
     * 先去 BaseAttrInfo中去查询 如果 能查询到再返回BaseAttrValue数据
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId) {

//     List<BaseAttrValue> list=manageService.getAttrValueList(attrId); 方式一 可能存在 脏数据
//        return  Result.ok(list);

        BaseAttrInfo baseAttrInfo = manageService.getAttrInfoById(attrId);
        if (baseAttrInfo != null) {
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            return Result.ok(attrValueList);
        }
        return Result.fail("没有相关的数据");

    }

    /**
     * /admin/product/saveAttrInfo
     * 保存-修改平台属性
     * 当id存在 说明已经回显数据了 是修改操作
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 根据分类Id 获取平台属性集合
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id) {

        List<BaseAttrInfo> list = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(list);
    }

    /**
     * 获取一级分类数据
     *
     * @return
     */
    @GetMapping("/getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> list = manageService.getCategory1();
        return Result.ok(list);
    }

//    获取二级分类数据   /admin/product/getCategory2/{category1Id}

    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> list = manageService.getCategory2(category1Id);
        return Result.ok(list);
    }

    /**
     * 获取三级分类数据
     *
     * @param category2Id
     * @return
     */
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        List<BaseCategory3> list = manageService.getCategory3(category2Id);
        return Result.ok(list);
    }


}
